package com.itheima.order.controller;

import com.itheima.annotation.IdempotentDemo;
import com.itheima.order.dao.OrderMapper;
import com.itheima.order.pojo.Order;
import com.itheima.order.service.OrderService;
import com.itheima.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("orderDemo")
public class OrderControllerDemo {

    @Autowired
    IdWorker idWorker;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    OrderService orderService;

    @GetMapping("getToken")

    public String getToken(){
        long id = idWorker.nextId();
        String token =String.valueOf(id);
        redisTemplate.opsForValue().set(token,"0",10, TimeUnit.MINUTES);
        return token;
    }

    //添加订单操作
    //添加拦截器进行校验
    @PostMapping("addOrder")
    @IdempotentDemo
    public String addOrder(@RequestBody Order order){

        int i = orderService.addOrder(order);
        if (i==1){
            return "success";
        }else{
            return "error";
        }
    }
}
